import moment from 'moment'
//import { indexsData } from '../static/index'
import { merge } from '../../helper/helper'
// import { userListFakeData } from '../static/index'

// Reducer
//默认state
const defaultState = {
    socialAccount: {},
    confirm: {},
    socialList: {
        type: 'vip',
        isPending: false,
        skip: 0,
        count: 0,
        chosen: [0, 1, 2, 3, 4, 5, 6, 7],
        data: [],
        currentPageIndex: 0,
        targetPageIndex: 0,
        direction: 'DESC',
        trigger: null,
        // onCheck: true, //隐藏左侧勾选框
        //checked: [],

    },
    sendPost: {
        tagName: "",
        previewError: "",
        userId: "",
        tagId: null,
        isPreviewPending: false
    },
    userDetail: {
        openId: "",
        platId: "",
        note: '',// 添加备注
        next_id: null,
        cur_next_id: null,
        page: 1,
        size: 10,
        msgType: "",
        isPending: false,  //加载动画
        eventDateRangeType: 0,     //选择的日期类型：过去7天，过去30天，过去90天，任意日期     
        data: {},                  //用户信息，从sociallist.data[]获得
        userInfo: {},
        customerOrder: {
            data: [],
            total: 0,
            moreOrderData: 2,
            page: 1,
            size: 6
        },
        order: {
            isPending: false,
            skip: 0,
            cont: 0,
            chosen: [0, 1, 2, 3, 4],
            onCheck: true,
            checked: [],
            data: [],
            currentPageIndex: 0,
            targetPageIndex: 0,
            direction: 'DESC',
            trigger: null
        },
        startTime: "",             //任意日期开始日期
        endTime: "",            //任意日期结束日期
        total: 0,               // 总的事件历史记录
        eventLists: [],            //事件列表
        moreEventMessageType: 2,
    },
    selector: {
        menuId: null,
        postType: null,
        postType1: null,
        postType2: null,
        isOpenSub1: false,
        isOpenSub2: false
    },
    currentVisit: {},
    isAddProfile: false,
    attributeList: [],
    visits: [],
    visitPage: 1,
    isHideCenterPopup: false,
    isHideBottomPopup: false,
    isLinkOrder: false,
    getVisitTime: Date.now(),
    newContact: {
        isAddContact: false,
        fetching: false,
        sizeList: [],
        clerkList: [],
        loading: false,
    }

};

//根据action.type来决定将哪些状态改为action.val
function reducer(state = defaultState, action) {
    switch (action.type) {
        case 'set-socialaccount':
            return merge(state, {
                socialAccount: action.val,
            });
        case 'set-cur-type':
            return merge(state, {
                socialList: merge(state.socialList, {
                    type: action.val
                })
            })
        case 'social-list-set-chosen':
            return merge(state, {
                socialList: merge(state.socialList, {
                    chosen: action.val
                })
            });
        case 'social-list-set-checked':
            return merge(state, {
                socialList: merge(state.socialList, {
                    checked: action.val
                })
            });
        case 'social-list-set-onCheck':
            return merge(state, {
                socialList: merge(state.socialList, {
                    onCheck: action.val
                })
            });
        case 'social-list-request':
            return merge(state, {
                socialList: merge(state.socialList, merge(action.val, {
                    isPending: true
                }))
            });
        case 'social-list-render-data':
            return merge(state, {
                socialList: merge(state.socialList, {
                    isPending: false,
                    data: action.val.data,
                    count: action.val.total
                })
            })
        case 'order-list-render-data':
            var data = action.val.data;
            for (let order of data) {
                order.billDate = moment(order.billDate).format('YYYY-MM-DD HH:mm:ss');
            }
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    order: merge(state.userDetail.order, {
                        isPending: false,
                        data: action.val.data,
                        count: action.val.total
                    })
                })
            });
        case 'order-list-request':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    order: merge(state.userDetail.order, merge(action.val, {
                        isPending: true
                    }))
                })
            });
        case 'order-list-set-checked':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    order: merge(state.userDetail.order, {
                        checked: action.val
                    })
                })
            });
        case 'social-list-trigger':
            return merge(state, {
                socialList: merge(state.socialList, {
                    trigger: (new Date().getTime())
                })
            });
        case 'social-list-status-message':
            return merge(state, {
                socialList: merge(state.socialList, {
                    statusMessage: action.val
                })
            });
        case 'social-list-set-setPageIndex':
            return merge(state, {
                socialList: merge(state.socialList, {
                    currentPageIndex: action.val,
                    targetPageIndex: action.val
                })
            });
        case 'order-list-set-setPageIndex':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    order: merge(state.userDetail.order, {
                        currentPageIndex: action.val,
                        targetPageIndex: action.val
                    })
                })
            });
        case 'event-list-render-data':
            var eventLists = [...state.userDetail.eventLists, ...action.val.data];
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    isPending: false,
                    eventLists: eventLists,
                    total: action.val.total
                })
            })
        case 'event-add-event-message-type':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    moreEventMessageType: action.val
                })
            })
        case 'set-page-index':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    page: action.val
                })
            })
        case 'event-list-request':
            return merge(state, {
                userDetail: merge(state.userDetail, merge(action.val, {
                    isPending: true
                }))
            });
        case 'clear-event-list-data':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    eventLists: [],
                    total: 0
                })
            })
        case 'select-date':
            if (action.startTime != state.userDetail.startTime || action.endTime != state.userDetail.endTime) {
                return merge(state, {
                    userDetail: merge(state.userDetail, {
                        startTime: action.startTime,
                        endTime: action.endTime,
                        eventLists: [],
                        cur_next_id: null,
                        next_id: null
                    })
                })
            }
            else {
                break
            }
        case 'select-eventdate-range':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    eventDateRangeType: action.val
                })
            })
        case 'set-open-id':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    openId: action.val
                })
            });
        case 'set-user-info':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    userInfo: action.val,
                    openId: action.val.customerId
                })
            })
        case 'set-order-data':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    customerOrder: merge(state.userDetail.customerOrder, {
                        data: action.val.data,
                        total: action.val.total
                    })
                })
            });
        case 'push-tag-name-to-data':
            let newArr2 = [...state.userDetail.data, action.val];
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    data: newArr2
                })
            })
        case 'delete-tag-from-data':
            let newArr1 = state.userDetail.data.slice();
            let curIndex = null;
            newArr1.forEach(function (item, index) {
                if (item.id == action.val) {
                    curIndex = index;
                    return;
                }
            });
            if (curIndex || curIndex == 0) {
                newArr1.splice(curIndex, 1);
            }
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    data: newArr1
                })
            })
        case 'set-event-lists':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    eventLists: action.val
                })
            })
        case 'set-msg-type':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    msgType: action.val
                })
            })
        case 'set-plat-id':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    platId: action.val
                })
            })
        case 'set-next-id':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    next_id: action.val
                })
            })
        case 'input-tag-name':
            return merge(state, {
                sendPost: merge(state.sendPost, {
                    tagName: action.val,
                    previewError: null
                })
            });
        case 'submit-tag-name-request':
            return merge(state, {
                sendPost: merge(state.sendPost, {
                    isPreviewPending: true
                })
            });
        case 'print-preview-message':
            return merge(state, {
                sendPost: merge(state.sendPost, {
                    previewMessage: action.val,
                    isPreviewPending: false
                })
            });
        case 'print-preview-error':
            return merge(state, {
                sendPost: merge(state.sendPost, {
                    previewError: action.val,
                    isPreviewPending: false
                })
            });
        case 'set-user-id':
            return merge(state, {
                sendPost: merge(state.sendPost, {
                    userId: action.val
                })
            })
        case 'set-menu-id':
            return merge(state, {
                selector: merge(state.selector, {
                    menuId: action.val
                })
            })
        case 'set-post-type-array':
            return merge(state, {
                selector: merge(state.selector, {
                    postTypeArry: action.val
                })
            })
        case 'set-selector-post-type':
            return merge(state, {
                selector: merge(state.selector, {
                    postType: action.val
                })
            })
        case 'set-selector-post-type1':
            return merge(state, {
                selector: merge(state.selector, {
                    postType1: action.val
                })
            })
        case 'set-selector-post-type2':
            return merge(state, {
                selector: merge(state.selector, {
                    postType2: action.val
                })
            })
        case 'set-tag-id':
            return merge(state, {
                sendPost: merge(state.sendPost, {
                    tagId: action.val
                })
            })

        case 'CHANGE_ATTRIBUTE':
            return merge(state, {
                attributeList: action.val,
            });
        case 'DELETE_ATTRIBUTE':
            state.attributeList.splice(action.val, 1);
            let newArr = [].concat(state.attributeList);
            let flag = true;
            if (newArr.length == 0) {
                flag = false;
            }
            return merge(state, {
                isAddProfile: flag,
                attributeList: newArr
            });
        case 'CHANGE_ADD_ATTR':
            var newCount = [...state.attributeList, { index: 0, secondIndex: 0, thirdIndex: 0, alsoStatus: 'and' }];

            return merge(state, {
                isAddProfile: true,
                attributeList: newCount
            });
        case 'edit-user-info':
            var name = action.val.name, value = action.val.value;
            var userInfo = state.userDetail.userInfo;
            switch (name) {
                case '姓名':
                    return merge(state, {
                        userDetail: merge(state.userDetail, {
                            userInfo: merge(state.userDetail.userInfo, {
                                customerName: value
                            })
                        })
                    });
                case '昵称':
                    return merge(state, {
                        userDetail: merge(state.userDetail, {
                            userInfo: merge(state.userDetail.userInfo, {
                                nickname: value
                            })
                        })
                    });

                case '性别':
                    return merge(state, {
                        userDetail: merge(state.userDetail, {
                            userInfo: merge(state.userDetail.userInfo, {
                                gender: value
                            })
                        })
                    });
                case '联系地址':
                    return merge(state, {
                        userDetail: merge(state.userDetail, {
                            userInfo: merge(state.userDetail.userInfo, {
                                address: value
                            })
                        })
                    });
                case '电话号码':
                    return merge(state, {
                        userDetail: merge(state.userDetail, {
                            userInfo: merge(state.userDetail.userInfo, {
                                mobile: value
                            })
                        })
                    });
                case '邮箱':
                    return merge(state, {
                        userDetail: merge(state.userDetail, {
                            userInfo: merge(state.userDetail.userInfo, {
                                email: value
                            })
                        })
                    });

            }
        case 'set-contact-status':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    userInfo: merge(state.userDetail.userInfo, {
                        isVip: action.val ? 1 : 0
                    })
                })
            });
        case 'set-visit-number':
            var visits = state.visits, newVisits = [];
            newVisits = visits.length > 0 ? visits.concat(action.val.data) : action.val.data;
            var audio = document.getElementsByTagName('audio')[0], times = 1;
            if (action.val.data.length > 0) {
                audio.addEventListener('ended', function () {
                    setTimeout(function () { if (times < action.val.data.length) { audio.play(); times++ } }, 1);
                }, false);
                audio.play();
            }
            return merge(state, {
                visits: newVisits,
                isHideBottomPopup: false
            })
        case 'set-visit-data':
            return merge(state, {
                currentVisit: action.val
            })

        case 'set-visit-page':
            return merge(state, {
                visitPage: action.val
            });
        case 'hide-center-popup':
            var obj = {};
            if (state.visits.length === 1) {
                obj = { visits: [], isHideCenterPopup: true, getVisitTime: Date.now() };
            } else {
                obj = { isHideCenterPopup: true }
            }
            return merge(state, obj);
        case 'hide-bottom-popup':
            return merge(state, {
                isHideBottomPopup: true,
                isHideCenterPopup: false,
                visits: [],
                getVisitTime: Date.now()
            })
        case 'link-user-order':
            return merge(state, {
                isLinkOrder: true
            })
        case 'close-link-order':
            return merge(state, {
                isLinkOrder: false
            })
        case 'refresh-deal-list':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    order: merge(state.userDetail.order, {
                        trigger: Date.now()
                    })
                })
            })
        case 'set-more-user-order':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    customerOrder: merge(state.userDetail.customerOrder, {
                        moreOrderData: action.val
                    })
                })
            })
        case 'set-get-visit-time':
            return merge(state, {
                getVisitTime: action.val
            })
        case 'change-keyword':
            return merge(state, {
                socialList: merge(state.socialList, {
                    keyword: action.val,
                    skip: 0
                })
            })
        case 'change-contact-note':
            return merge(state, {
                userDetail: merge(state.userDetail, {
                    note: action.val
                })
            })
        case 'trigger-new-contact':
            return merge(state, {
                newContact: { isAddContact: !state.newContact.isAddContact }
            })
        case 'set-all-size':
            return merge(state, {
                newContact: merge(state.newContact, {
                    sizeList: action.val
                })
            })
        case 'set-all-clerk':
            return merge(state, {
                newContact: merge(state.newContact, {
                    clerkList: action.val,
                    fetching: !state.newContact.fetching
                })
            })
        case 'change-fetching-status':
            return merge(state, {
                newContact: merge(state.newContact, {
                    fetching: !state.newContact.fetching
                })
            })
        case 'change-new-contact-loading':
            return merge(state, {
                newContact: merge(state.newContact, {
                    loading: !state.newContact.loading
                })
            })

    }

    return state
}

export default reducer